local ts = require("ts")
local json = ts.json --使用 JSON 模块前必须插入这一句
local sz = require("sz")
local socket = require ("socket");
local http = require("szocket.http")
require("TSLib")

header_send = {typeget = "ios"}
ts.setHttpsTimeOut(60) --安卓不支持设置超时时间

--全局函数
logcount = 0; --从零开始
uuid = string.sub(getDeviceID(),-3,-1); --截取连接手机的唯一标识号
--手机状态
devstatus = 0; --0是空闲，1是忙碌

--判断程序是否在运行
function isrunapp()
	if frontAppBid() =="com.tencent.ied.app.lolbible" then
		return true;
	else
		return false;
	end
end

--killapp
function killapp()
	closeApp("com.tencent.ied.app.lolbible");
	closeApp("*",1);--关于所有后台
end

--runapp
function runapps()
	r = runApp("com.tencent.ied.app.lolbible");
	mSleep(1000*3);--启动等待一会儿
	return r;
end


--带日期参数的日志函数
function nlogc(x)
	logcount=logcount+1;
	nLog(uuid .. "_" .. logcount .. x);
end

function compare_color_point(x,y,r,g,b,sim)--判断颜色点，内部调用
	local lr,lg,lb;
	lr,lg,lb = getColorRGB(x, y);
	
	if math.abs(lr-r) >sim then
		return false;
	end
	if math.abs(lg-g) >sim then
		return false;
	end
	if math.abs(lb-b) >sim then 
		return false;
	end
	return true;
end

--装载掌上英雄联盟
function _loadprg()
	nlogc("进_logdingprg");
	local appSign = 0;x=-1;y=-1;
	killapp();
	mSleep(1800);
	keepScreen(false);
	repeat
		nlogc("进..启动程序")
		appSign = runapps();
		mSleep(800);
		nlogc("启动程序.."..appSign)
	until appSign==0;
	repeat
		nlogc("进..英雄联盟");
		mSleep(800);
		nlogc("装载英雄联盟盒子");
	until compare_color_point(267,917,208,172,102,5);
	nlogc("装载完毕");--已经到主界面了
end

function startprg()--启动程序
	
	mSleep(1000);
	_loadprg();--装载程序
end

--短按
function _click(x,y,z)
	touchDown(0, x, y);
	mSleep(30);
	touchUp(0, x, y);
	mSleep(z);
end

--长按
function _longclick(x,y,z)
	touchDown(0, x, y);
	mSleep(800);
	touchUp(0, x, y);
	mSleep(z)
end


--请求odejs
function req_nodejs()
	--发送请求
	status_resp, header_resp,body_resp = ts.httpGet("http://10.0.0.6:3000/admin/getdata?devstatus=" .. devstatus .. "&devid=" .. uuid, header_send, body_send)
	
	
	local tmp = json.decode(body_resp);
	qqnum = tmp.qqnum; --账号
	qqpws = tmp.qqpws;	--密码
	
	nlogc(qqnum .. ":" ..qqpws);
	--如果有账号和密码，调用登录方法，并且上传图片
	if status_resp == 200 then
		devstatus=1; --修改状态为忙碌
		nlogc(qqnum .. ":" ..qqpws);
		nlogc("开始登录等操作");
		
		startprg();
		
	if compare_color_point(668,108,188,156,118,5)~=true then
		_click(267,923,600);
		
		repeat
			nlogc("等待登录界面出来");
			mSleep(300);
		until compare_color_point(201,561,18,183,245,5)
			nlogc("可以输入密码了");
		_click(80,313,300);
		inputText(qqnum);
		mSleep(800);
		_click(82,459,300);
		inputText(qqpws);
		mSleep(800);
		_click(358,599,300);
	end

	repeat
		mSleep(300);
	until compare_color_point(575,1075,121,138,140,5);
	mSleep(3000);
	nlogc("已到主界面,进入战绩");
	_click(575,1075,600);

	nlogc("已到了战绩界面")
	mSleep(3000);
	snapshot("zjs.png", 238,410, 390,446);
	snapshot("sl.png", 469,410, 600,446);

	nlogc("点击能力截图");
	_click(296,364,400);
	repeat
		mSleep(300)
	until compare_color_point(296,364,48,172,166,5);
	--开始能力截图
	nlogc("能力截图");
	snapshot("lbx.png",170,440,472,734);
end
--上传图片
--url = "http://10.0.0.3:3333/upload";
url="http://10.0.0.6:3000/admin/uploadpic";
local _file1 = [[--abcd]]..'\r\n'..[[Content-Disposition: form-data; name="photo"; filename="sl.png"]]..'\r\n'..[[Content-Type: image/png]]..'\r\n\r\n'
local _file2 = [[--abcd]]..'\r\n'..[[Content-Disposition: form-data; name="photo"; filename="zjs.png"]]..'\r\n'..[[Content-Type: image/png]]..'\r\n\r\n'
local _file3 = [[--abcd]]..'\r\n'..[[Content-Disposition: form-data; name="photo"; filename="lbx.png"]]..'\r\n'..[[Content-Type: image/png]]..'\r\n\r\n'
imgupload2(url, userPath() .. "/res/" .. "sl.png",_file1);
imgupload2(url, userPath() .. "/res/" .. "zjs.png",_file2);
imgupload2(url, userPath() .. "/res/" .. "lbx.png",_file3);
--退出登陆
logout();	
	end
	
--退出登录
function logout()
	
	_click(66,79,600);
	repeat
		mSleep(300);
	until compare_color_point(258,91,198,160,89,5);

	_click(78,1072,600);

	repeat
		mSleep(300);
	until compare_color_point(307,84,186,154,108,5);
	--滑动
	moveTowards(272,937,90,300);

	mSleep(600);
	_click(276,1032,600);
	mSleep(600)
	_click(304,926,600);

	devstatus=0;
end

function imgupload()
	--上传第一张图片
	local file=io.open(userPath() .. "/res/" .. "zjs.png");
	local msg="";
	if file then
		 msg=file:read("*a");
		file:close()
	end
	tomcatUrl="http://10.0.0.3:8080/uploadtest/test?id=".. qqnum .. "&name=" .. "zjs.png";

	httpPost(tomcatUrl,msg)

	--上传第二张图片
	local file=io.open(userPath() .. "/res/" .. "sl.png");
	local msg="";
	if file then
		 msg=file:read("*a");
		file:close()
	end
	tomcatUrl="http://10.0.0.3:8080/uploadtest/test?id=".. qqnum .. "&name=" .. "sl.png";

	httpPost(tomcatUrl,msg)

	--上传第三张图片
	local file=io.open(userPath() .. "/res/" .. "lbx.png");
	local msg="";
	if file then
		 msg=file:read("*a");
		file:close()
	end
	tomcatUrl="http://10.0.0.3:8080/uploadtest/test?id=".. qqnum .. "&name=" .. "lbx.png";

	httpPost(tomcatUrl,msg)
	nLog("图片发送完成")
end

--无限循环轮询
function lunxun()
	repeat
		nlogc("开始请求");
		req_nodejs();
	until (devstatus==0)
end

function imgupload2(_url,path,imageName)
	local respbody = {}
    --local _file = [[--abcd]]..'\r\n'..[[Content-Disposition: form-data; name="photo"; filename="haha.png"]]..'\r\n'..[[Content-Type: image/png]]..'\r\n\r\n'
    --local http=require("socket.http");
	local _file = imageName;
    local _end ='\r\n'..[[--abcd--]]..'\r\n'
    local reqfile= io.open(path)
    if reqfile == nil then
    print("file not found")
    return
    end
    local size = io.open(path):seek("end")
    if  size ==  0 then
	print("empty file")
	return
    end
    local info = "[*] uploading "..path.." to url : ".._url.."  size:  "..tostring(size).."bytes"    
print(info)
    local  res , code , rsp_body = http.request {
        method = "POST",
        url = _url,
        headers = {
            ["Content-Type"] =  "multipart/form-data;boundary=abcd",
            ["Content-Length"] = #_file+size+#_end,
        },
        source = ltn12.source.cat(ltn12.source.string(_file),ltn12.source.file(reqfile),ltn12.source.string(_end)),
        sink = ltn12.sink.table(respbody)
    }
if code  == 200 then
	print('[+] uploading process succeed ')
	else
        print("[-] uploading failed code".. tostring(code));
end
end

lunxun();






